package com.seeyon.apps.kk.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.organization.OrgConstants;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgEntity;
import com.seeyon.ctp.organization.bo.V3xOrgLevel;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.dao.OrgHelper;
import com.seeyon.ctp.organization.po.OrgLevel;
import com.seeyon.ctp.organization.po.OrgMember;
import com.seeyon.ctp.organization.po.OrgPost;
import com.seeyon.ctp.organization.po.OrgUnit;
import com.seeyon.ctp.util.DBAgent;
import com.seeyon.ctp.util.Strings;

/**
 * @author 
 * @date 2015-8-18下午9:55:04
 * @Copyright(c) Beijing Seeyon Software Co.,LTD
 */
public class SyncOrgDaoImpl implements SyncOrgDao {

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("rawtypes")
    public V3xOrgLevel getLevelByCode(Long accountId, String property, Object feildvalue) {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();
        hql.append(" FROM " + OrgLevel.class.getSimpleName());
        hql.append(" WHERE deleted=false");
        if(accountId != null) {
            hql.append(" AND orgAccountId=:accountId");
            params.put("accountId", accountId);
        }
        if(StringUtils.isNotBlank(property) && !"null".equals(property)) {
            hql.append(" AND ").append(property).append("=:feildvalue");
            params.put("feildvalue", feildvalue);
        }
        hql.append(" ORDER BY levelId ASC ");
        List list = DBAgent.find(hql.toString(), params);
        if(!list.isEmpty() && list.get(0) != null) {
            return (V3xOrgLevel)OrgHelper.poTobo((OrgLevel)list.get(0));
        } else {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("rawtypes")
    public V3xOrgMember getMemberByCode(Long accountId, String property, Object feildvalue) {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();
        hql.append("SELECT m ");
        hql.append(" FROM OrgMember as m");
        hql.append(" WHERE ");
        hql.append(" m.deleted=false AND m.admin=false AND m.virtual=false AND m.assigned=true");
        if(accountId != null) {
            hql.append(" AND m.orgAccountId=:accountId ");
            params.put("accountId", accountId);
        }
        if(Strings.isNotBlank(property) && !"null".equals(property)) {
            hql.append(" AND ").append(property).append("=:feildvalue");
            params.put("feildvalue", feildvalue);
        }
        hql.append(" ORDER BY m.sortId ASC");
        List list = DBAgent.find(hql.toString(), params);
        if(!list.isEmpty() && list.get(0) != null) {
            return (V3xOrgMember)OrgHelper.poTobo((OrgMember)list.get(0));
        } else {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("rawtypes")
    public V3xOrgPost getPostByCode(Long accountId, String property, Object feildvalue) {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();
        hql.append(" FROM " + OrgPost.class.getSimpleName());
        hql.append(" WHERE deleted=false");
        if(accountId != null) {
            hql.append(" AND orgAccountId=:accountId");
            params.put("accountId", accountId);
        }
        if(StringUtils.isNotBlank(property) && !"null".equals(property)) {
            hql.append(" AND ").append(property).append("=:feildvalue");
            params.put("feildvalue", feildvalue);
        }
        hql.append(" ORDER BY sortId ASC");
        List list = DBAgent.find(hql.toString(), params);
        if(!list.isEmpty() && list.get(0) != null) {
            return (V3xOrgPost)OrgHelper.poTobo((OrgPost)list.get(0));
        } else {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("rawtypes")
    public V3xOrgDepartment getDeptByCode(Long accountId, String property, Object feildvalue) {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();
        hql.append("SELECT m ");
        hql.append(" FROM " + OrgUnit.class.getSimpleName() + " as m ");
        hql.append(" WHERE m.deleted=false ");
        hql.append(" AND m.type=:type ");
        params.put("type", OrgConstants.UnitType.Department.name());
        if(accountId != null && accountId != V3xOrgEntity.VIRTUAL_ACCOUNT_ID) {
            hql.append(" AND m.orgAccountId=:orgAccountId ");
            params.put("orgAccountId", accountId);
        }
        if(StringUtils.isNotBlank(property) && !"null".equals(property)) {
            hql.append(" and m.").append(property).append("=:feildvalue");
            params.put("feildvalue", feildvalue);
        }
        hql.append(" ORDER BY m.sortId ASC");
        List list = DBAgent.find(hql.toString(), params);
        if(!list.isEmpty() && list.get(0) != null) {
            return (V3xOrgDepartment)OrgHelper.poTobo((OrgUnit)list.get(0));
        } else {
            return null;
        }
    }
    
    @Override
    public CtpAffair getAffair(Long summaryId, Long userId) throws BusinessException{
    	StringBuilder sb = new StringBuilder("from CtpAffair");
    	sb.append(" where objectId=:summaryId and memberId=:userId");
    	sb.append(" and state=:state order by receiveTime desc");//and subState=:sub 
    	Map params = new HashMap();
    	params.put("state", 6);
//    	params.put("sub", 15);
    	params.put("summaryId", summaryId);
    	params.put("userId", userId);
    	List<CtpAffair> list = DBAgent.find(sb.toString(), params);
    	if(CollectionUtils.isNotEmpty(list)){
    		return list.get(0);
    	}
    	return null;
    }
}
